package gui;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;

import controllers.OBLSettings;

import java.awt.event.KeyEvent;
/**
 * This class representing a Subscriber Edit profile Gui
 * Date: 25/12/2012
 * @author Roman
 *
 */
public class SubscriberEditProfileUI extends JPanel 
{
	 
	private static final long serialVersionUID = 1L;
	
	JButton btnLogout;
	JButton btnExit;
	private JLabel lblOblSubscriber;
	private JSeparator separator;
	private JSeparator separator_1;
	private JLabel lblEditDetails;
	private JLabel lblPhoneNumber;
	 JTextField phoneTextField;
	private JLabel lblEmail;
	 JTextField EmailTextField;
	private JLabel lblNewLabel;
	 JTextField AddressTextField;
	private JLabel lblPassword;
	JTextField PasswordTextField;
	private JButton btnConfirm;
	private JButton btnBack;
	private SubscriberUI subUI = null ;
	
	public SubscriberEditProfileUI(SubscriberUI fatherPanel)
	{
		super();
		initialize();
		this.subUI = fatherPanel;
	}
	/**
	 * This method initializes the panel
	 */
	private void initialize()
	{
		setLayout(null);
		this.setSize(OBLSettings.pane2Size[0],OBLSettings.pane2Size[1]);
		separator = new JSeparator();
		separator.setBounds(-17, 57, 855, 20);
		add(separator);
		
		lblOblSubscriber = new JLabel("OBL Edit Profile");
		lblOblSubscriber.setFont(new Font("Tahoma", Font.BOLD, 20));
		lblOblSubscriber.setBounds(310, 11, 179, 35);
		add(lblOblSubscriber);
		
		btnLogout = new JButton("Logout");

		btnLogout.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnLogout.setBounds(614, 11, 83, 41);
		add(btnLogout);
		
		btnExit = new JButton("Exit");

		
		btnExit.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnExit.setBounds(707, 11, 83, 41);
		add(btnExit);
		
		separator_1 = new JSeparator();
		separator_1.setBounds(-15, 512, 837, 35);
		add(separator_1);
		
		lblEditDetails = new JLabel("Edit Details :");
		lblEditDetails.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblEditDetails.setBounds(22, 70, 130, 41);
		add(lblEditDetails);
		
		lblPhoneNumber = new JLabel("phone number :");
		lblPhoneNumber.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblPhoneNumber.setBounds(119, 111, 110, 26);
		add(lblPhoneNumber);
		
		phoneTextField = new JTextField();
		phoneTextField.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		phoneTextField.setFont(new Font("Tahoma", Font.PLAIN, 14));
		phoneTextField.setBounds(234, 116, 165, 24);
		add(phoneTextField);
		phoneTextField.setColumns(10);
		
		lblEmail = new JLabel("email :");
		lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblEmail.setBounds(119, 148, 83, 26);
		add(lblEmail);
		
		EmailTextField = new JTextField();
		EmailTextField.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		EmailTextField.setFont(new Font("Tahoma", Font.PLAIN, 14));
		EmailTextField.setBounds(234, 150, 165, 24);
		add(EmailTextField);
		EmailTextField.setColumns(10);
		
		lblNewLabel = new JLabel("address :");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblNewLabel.setBounds(119, 190, 95, 26);
		add(lblNewLabel);
		
		AddressTextField = new JTextField();
		AddressTextField.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		AddressTextField.setFont(new Font("Tahoma", Font.PLAIN, 14));
		AddressTextField.setBounds(234, 192, 165, 24);
		add(AddressTextField);
		AddressTextField.setColumns(10);
		
		lblPassword = new JLabel("password :");
		lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblPassword.setBounds(119, 227, 101, 26);
		add(lblPassword);
		
		PasswordTextField = new JTextField();
		PasswordTextField.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		PasswordTextField.setFont(new Font("Tahoma", Font.PLAIN, 14));
		PasswordTextField.setBounds(234, 229, 165, 24);
		add(PasswordTextField);
		PasswordTextField.setColumns(10);
		
		btnConfirm = new JButton("Confirm");
		btnConfirm.setMnemonic(KeyEvent.VK_ENTER);
	
		btnConfirm.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnConfirm.setBounds(317, 315, 165, 41);
		add(btnConfirm);
		
		btnBack = new JButton("Back");

		btnBack.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnBack.setBounds(26, 11, 83, 41);
		add(btnBack);

//================================================================		
//		                 Action Listeners
//================================================================	
		btnConfirm.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				if( getPassword().isEmpty() & getPhone().isEmpty() & getEmail().isEmpty() & getAddress().isEmpty())
					JOptionPane.showMessageDialog( WelcomeUI.wui, "All the fields are empty .\n Please fill at " +
							"least one of the fields .","Warning",JOptionPane.WARNING_MESSAGE);
				else
				{
					int result = subUI.subscriberUIP.updateSubProfile(subUI.subscriber.getID(),getPassword() , getPhone() , getEmail() , getAddress());
					if(result==0)
						JOptionPane.showMessageDialog( WelcomeUI.wui, "There was an error in update" +
								", please try again ","Warning",JOptionPane.WARNING_MESSAGE);
					else
					{
						JOptionPane.showMessageDialog( WelcomeUI.wui, " Update succeded ","Succes",JOptionPane.INFORMATION_MESSAGE);
						if(!getEmail().isEmpty())
							subUI.subscriber.setEmail(getEmail());
						if(!getAddress().isEmpty())
							subUI.subscriber.setAddress(getAddress());
						if(!getPhone().isEmpty())
							subUI.subscriber.setPhone(getPhone());
						if(!getPassword().isEmpty())
							subUI.subscriber.setPassword(getPassword());
						
					}
				}
				
			}
		});

//================================================================			
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				setPhone("");
				setEmail("");
				setAddress("");
				setPassword("");
				WelcomeUI.wui.switchPanel("OBL System - Subscriber",subUI ,OBLSettings.pane2Size[0], OBLSettings.pane2Size[1]);		
			}
		});	

	}//initialize
	
//===================================================================	
//					Getters & Setters
//===================================================================	
	public String getPhone()
	{
		return this.phoneTextField.getText();
	}
	public void setPhone(String Phone)
	{
		this.phoneTextField.setText(Phone); 
	}
	public String getEmail()
	{
		return this.EmailTextField.getText();
	}
	public void setEmail(String email)
	{
		this.EmailTextField.setText(email); 
	}
	public String getAddress()
	{
		return this.AddressTextField.getText();
	}
	public void setAddress(String address)
	{
		this.AddressTextField.setText(address); 
	}
	public String getPassword()
	{
		return this.PasswordTextField.getText();
	}
	public void setPassword(String pass)
	{
		this.PasswordTextField.setText(pass); 
	}
	
	public void paintComponent(Graphics g) 
	{
		g.drawImage((OBLSettings.IMG_DEFAULT_BACKGROUND).getImage(), 0, 0, OBLSettings.pane2Size[0], OBLSettings.pane2Size[1],null);
		
	}
}//class
